package cn.jinjimi.app.custom.ui.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v4.content.ContextCompat;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.jinjimi.base.util.DateUtils;
import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.custom.entity.Insurance;
import cn.jinjimi.app.interfaces.DataSetChangeListener;
import cn.jinjimi.framework.router.Router;
import cn.jinjimi.app.main.adapter.extend.MyBaseAdapter;

/**
 * Created by Roy on 16/6/8.
 */
public class InsureListAdapter extends MyBaseAdapter<Insurance> {
    private Context mContext;

    private Paint mPaint;
    private Paint mPaint2;
    private int titleLength;

    private DataSetChangeListener dataSetChangeListener;

    public InsureListAdapter(Context context, DataSetChangeListener listener) {
        mContext = context;
        mPaint = new Paint();
        mPaint2 = new Paint();
        mPaint.setTextSize(mContext.getResources().getDimension(R.dimen.font_14));
        mPaint2.setTextSize(mContext.getResources().getDimension(R.dimen.font_11));

        this.dataSetChangeListener = listener;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        if (dataSetChangeListener != null)
            dataSetChangeListener.dataChange();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final InsureHolder holder;

        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_insure_follow_plan_list, null);
            holder = new InsureHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (InsureHolder) convertView.getTag();
        }

        final Insurance item = getItem(position);
        if (item != null) {

            if (titleLength == 0) {
                ViewTreeObserver vto2 = holder.tvTitle.getViewTreeObserver();
                vto2.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        holder.tvTitle.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        titleLength = holder.tvTitle.getWidth();
                        holder.setTitle(item);
                    }
                });
            } else {
                holder.setTitle(item);
            }

            // 非手工添加的保单有被保人姓名
            if (StringUtils.isNotEmpty(item.getInsured_name()) && item.getChannel() != Insurance.Channel_hand) {
                holder.tvInsureName.setText(String.format(mContext.getString(R.string.custom_insure_name), item.getInsured_name()));
                holder.tvInsureName.setVisibility(View.VISIBLE);
            } else {
                holder.tvInsureName.setVisibility(View.GONE);
            }

            holder.tvInsureEffectDate.setText(String.format(mContext.getString(R.string.custom_insure_time), DateUtils.formatYYYYMMDD(item.getEffect_date())));

            holder.cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (item.getView_url() != null) {
                        Router.create(mContext, item.getView_url()).open();
                    }
                }
            });
        }
        return convertView;
    }

    public class InsureHolder {

        public RelativeLayout cardView;
        public TextView tvTitle;
        public TextView tvInsureName;
        public TextView tvInsureEffectDate;

        public InsureHolder(View view) {
            cardView = (RelativeLayout) view.findViewById(R.id.card_view);
            tvTitle = (TextView) view.findViewById(R.id.tv_title);
            tvInsureName = (TextView) view.findViewById(R.id.tv_sec_title);
            tvInsureEffectDate = (TextView) view.findViewById(R.id.tv_third_title);
        }

        /* 设置标题 */
        public void setTitle(Insurance item) {
            String channel = "";
            switch (item.getChannel()) {
                case Insurance.Channel_hand:
                    channel = mContext.getString(R.string.custom_insure_hand);
                    break;
                case Insurance.Channel_online:
                    channel = mContext.getString(R.string.custom_insure_online);
                    break;
                case Insurance.Channel_give:
                    channel = mContext.getString(R.string.custom_insure_give);
                    break;
            }

            float widthName = mPaint.measureText(item.getName());
            float widthChannel = mPaint2.measureText("  " + channel);

            if (StringUtils.isEmpty(item.getName()))
                tvTitle.setHint(mContext.getString(R.string.custom_insure_default_name, DateUtils.formatYYYYMMDD(item.getEffect_date())));
            else {
                SpannableStringBuilder sb = new SpannableStringBuilder(item.getName() + "  ");
                if (widthName < titleLength && widthName + widthChannel > titleLength)
                    sb.append("\n");
                sb.append(channel);
                sb.setSpan(new AbsoluteSizeSpan(11, true), item.getName().length(), sb.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                sb.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.text_light_black)), item.getName().length(), sb.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                tvTitle.setText(sb);
            }
        }
    }
}